package com.zlz.controller;

import com.zlz.entity.Book;
import com.zlz.repository.BookRepository;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.ScrolledPage;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * @Author: 曾令忠
 * @Description:
 * @Date: 2020/8/3 0003-下午 3:54
 */
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookRepository bookRepository;
    @Autowired
    private ElasticsearchTemplate template;

    @RequestMapping("/like")
    public List<Book> like(){
        long begin = System.currentTimeMillis();
        List<Book> books = bookRepository.findByNameLike("java 入门");
        long end = System.currentTimeMillis();
        System.out.println("耗时:"+(end-begin)+"毫秒");
        return books;
    }

    //分页查询
    @RequestMapping("/all")
    public List<Book> all() {
        Pageable pageable = PageRequest.of(0, 10);
        Iterable<Book> iterable = bookRepository.findAll(pageable);
        List<Book> books = new ArrayList<Book>();
        iterable.forEach(books::add);
        return books;
    }

    //以 id 查询
    @RequestMapping("/id")
    public Book id() {
        Optional<Book> optional = bookRepository.findById(1);
        return optional.get();
    }

    //测试查询
    @RequestMapping("/test")
    public List<Book> test(Integer pageNum,Integer pageSize) {
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(QueryBuilders.rangeQuery("id").gte(1).lte(20))
                .withPageable(PageRequest.of(0, 10)) .build();
        ScrolledPage<Book> scrolledPage = (ScrolledPage<Book>)template
                .startScroll(5000, searchQuery, Book.class);
        System.out.println("总数:"+scrolledPage.getTotalElements());
        for(int i = 0; i < 1; i++){
            //翻页
            template.continueScroll(scrolledPage.getScrollId(), 5000, Book.class);
        }
        System.out.println("当前:"+scrolledPage.getContent().size());
        List<Book> books = scrolledPage.getContent();
        template.clearScroll(scrolledPage.getScrollId());
        return books;
    }

    @RequestMapping("/must")
    public List<Book> must() {
        WildcardQueryBuilder wildcardQueryBuilder = QueryBuilders
                .wildcardQuery("name", "*java*");
        RangeQueryBuilder rangeQueryBuilder = QueryBuilders
                .rangeQuery("id").gte(0).lte(200);
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.should(wildcardQueryBuilder);
        boolQueryBuilder.must(rangeQueryBuilder);
        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withQuery(boolQueryBuilder)
                .withPageable(PageRequest.of(0, 100)) .build();
        ScrolledPage<Book> scrolledPage = (ScrolledPage<Book>)template.startScroll(5000, searchQuery, Book.class);
        System.out.println("总数:"+scrolledPage.getTotalElements()); for(int i=0;i < 1;i++){
            //翻页
            template.continueScroll(scrolledPage.getScrollId(), 5000, Book.class);
        }
        System.out.println("当前:"+scrolledPage.getContent().size());
        List<Book> books = scrolledPage.getContent();
        template.clearScroll(scrolledPage.getScrollId());
        return books;
    }
}
